import UnityPy
from utils.JSON的输入和输出 import 写入json, 读取json  # noqa

file_map = {
    "DLG_Indx": "DLG_Indx",
    "DLG_Text": "DLG_Text",
}


def extract_file(src, dst):
    env = UnityPy.load(src)
    for obj in env.objects:
        if obj.type.name == "TextAsset":
            data = obj.read()
            target_name: str = data.name
            if not target_name in file_map.keys(): continue
            print(data.name)
            with open(f"output/加密后/{file_map[target_name]}.txt", "rb") as f:
                data.script = f.read()
            data.save()
    with open(dst, "wb") as f:  # 其实就是拿到文件名 写入而已
        f.write(env.file.save())


extract_file('data/data.unity3d', "output/注入到data.unity3d/data.unity3d")